package com.apes.scm.masterdata.party.service;

import com.apes.framework.api.DomainService;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.scm.masterdata.area.model.Area;
import com.apes.scm.masterdata.area.repository.AreaRepository;
import com.apes.scm.masterdata.party.model.Party;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.party.model.SupplierShip;
import com.apes.scm.masterdata.party.repository.PartyIdentificationRepository;
import com.apes.scm.masterdata.party.repository.PartyRoleRepository;
import com.apes.scm.masterdata.party.repository.SupplierRepository;
import com.apes.scm.masterdata.party.repository.SupplierShipRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * 功能：供应商信息
 *
 * @author xul
 * @create 2018-01-23 14:35
 */
@Service("supplierShipService")
public class SupplierShipService extends DomainService {
    @Autowired
    SupplierShipRepository supplierShipRepository;
    @Autowired
    CorporateSerive corporateSerive;
    @Autowired
    AreaRepository areaRepository;
    public SupplierShip save(SimpleRequest request) {

        SupplierShip supplierShip = request.getO(SupplierShip.class);
        // 设置注册省份、注册城市、办公省份、办公城市
        // 获取注册区县
        Area regCounty = supplierShip.getCounty();// 注册地址.区县
        // 获取注册城市编码
        String regCityId = regCounty.getParent();
        // 查找注册城市
        Area regCity = areaRepository.findOne(regCityId);
        supplierShip.setCity(regCity); // 注册地址.城市
        // 设置注册城市
        String regProvinceId = regCity.getParent();
        Area regProvince = areaRepository.findOne(regProvinceId);
        supplierShip.setProvince(regProvince); //注册地址.省份
        supplierShip.setState("create");
        return supplierShipRepository.saveAndFlush(supplierShip);
    }


    // 更新供应商信息
    public SupplierShip update(SimpleRequest request) {
        SupplierShip supplierShip = request.getO(SupplierShip.class);
        // 设置注册省份、注册城市、办公省份、办公城市
        // 获取注册区县
        Area regCounty = supplierShip.getCounty();// 注册地址.区县
        // 获取注册城市编码
        String regCityId = regCounty.getParent();
        // 查找注册城市
        Area regCity = areaRepository.findOne(regCityId);
        supplierShip.setCity(regCity); // 注册地址.城市
        // 设置注册城市
        String regProvinceId = regCity.getParent();
        Area regProvince = areaRepository.findOne(regProvinceId);
        supplierShip.setProvince(regProvince); //注册地址.省份
        supplierShip.setState("create");
        return supplierShipRepository.saveAndFlush(supplierShip);
    }
    public SupplierShip delete(SimpleRequest request) {
        String id = request.get("id");
        Integer versions = request.get("version");
        SupplierShip supplierShip = supplierShipRepository.findOne(Long.valueOf(id));
        if (supplierShip == null) throw new RuntimeException("供应商发货点【" + id + "】在系统中不存在。");
        if (supplierShip.getVersion() != versions) throw new RuntimeException("记录已更新或已删除，请重新提交！");
        supplierShip.setState("delete");
        return supplierShipRepository.saveAndFlush(supplierShip);
    }

}
